package com.lemner.ke.activity;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.lemner.ke.R;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.fragment.KnowledgeFragment;
import com.lemner.ke.fragment.LibraryFragment;
import com.lemner.ke.util.SeeMoreListener;
import com.lemner.ke.util.SpUtils;

import org.w3c.dom.Text;

/**
 * 首页
 *
 * @author raintea
 * @date 2018/9/18
 */
public class HomeActivity extends BaseActivity implements SeeMoreListener {

    LinearLayout llTopbar;
    ImageView ivMine, ivMessage;
    EditText etSearch;
    FrameLayout flView;
    RadioGroup rg;

    KnowledgeFragment knowledgeFragment;
    LibraryFragment libraryFragment;

    private int loginStatus;
    private String userIds;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_home;
    }

    @Override
    public void initView() {

        llTopbar = findViewById(R.id.ll_home_topbar);
        initImmersionBarForTopBar(llTopbar);
        ivMine = findViewById(R.id.iv_home_mine);
        etSearch = findViewById(R.id.et_home_search);
        ivMessage = findViewById(R.id.iv_home_message);
        flView = findViewById(R.id.fl_home_frame_view);
        rg = findViewById(R.id.rg_home);
    }

    @Override
    public void initEvent() {
        knowledgeFragment = KnowledgeFragment.getInstance();
        libraryFragment = LibraryFragment.getInstance();
        knowledgeFragment.setListener(this);
        ivMine.setOnClickListener(this);
        ivMessage.setOnClickListener(this);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                switch (checkedId) {
                    case R.id.rb_home_knowledge:
                        transaction.replace(R.id.fl_home_frame_view, knowledgeFragment);
                        break;
                    case R.id.rb_home_library:
                        transaction.replace(R.id.fl_home_frame_view, libraryFragment);
                        break;
                    default:
                }
                transaction.commit();
            }
        });

        rg.check(R.id.rb_home_knowledge);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().equals("")) {
                    libraryFragment.layoutright.setVisibility(View.VISIBLE);
                } else {
                    if (libraryFragment.layoutright!=null){
                        libraryFragment.layoutright.setVisibility(View.INVISIBLE);
                    }
                }
                libraryFragment.keywords = charSequence.toString();
                libraryFragment.cur_page = 1;
                libraryFragment.upDateList("", "", charSequence.toString(), "createtime");
                rg.check(R.id.rb_home_library);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initSetting() {
        setTopBarEnable(false);
    }


    @Override
    public void onClick(View v) {
        loginStatus = SpUtils.getInt(this, SpUtils.LOGIN_STATUS);
        userIds = SpUtils.getString(this, SpUtils.USERIDS);
        switch (v.getId()) {
            case R.id.iv_home_mine:
                if (loginStatus == SpUtils.LOGIN) {
                    Intent passIntent = new Intent(this, MineActivity.class);
                    passIntent.putExtra("ids", userIds);
                    startActivity(passIntent);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.iv_home_message:
                if (loginStatus == SpUtils.LOGIN) {
                    Intent passIntent = new Intent(this, SystemMessageActivity.class);
                    passIntent.putExtra("ids", userIds);
                    startActivity(passIntent);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            default:
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        loginStatus=SpUtils.getInt(this, SpUtils.LOGIN_STATUS);
    }

    @Override
    public void seeMore() {
        rg.check(R.id.rb_home_library);
    }
}
